package Controller1;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseDragEvent;
import javafx.stage.Stage;

import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import Dao.goodsDao;
import Dao.orderDetailDao;
import Dao.sellorderDao;
import Model1.goods;
import Model1.orderDetail;
import Model1.sellorder;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.TableView;

import javafx.scene.control.TableColumn;

public class MainMenuController {
	public List<orderDetail> checkgood=new ArrayList<>();
	
	@FXML
	private Button beef_button;
	@FXML
	private Button noodles_button;
	@FXML
	private Button juice_button;
	@FXML
	private Button rice_button;
	@FXML
	private Button snack_button;
	@FXML
	private Button order;
	@FXML
	private TableView Mealtable= new TableView<>();
	@FXML
	private TableColumn name=new TableColumn("name");
	@FXML
	private TableColumn price=new TableColumn("price");
	@FXML
	private TextField goods_name;
	@FXML
	private TextField goods_price;
	@FXML
	private TextField sales;
	@FXML
	private TableView orderTable= new TableView<>();
	@FXML
	private TableColumn checkedMeal=new TableColumn("checkedMeal");
	@FXML
	private TableColumn amount=new TableColumn("amount");
	@FXML
	private Button Update;
	@FXML
	private Button checkdata;
	public int count=0;
	public void initialize() {
		//Style.getItems().addAll("奶茶类", "果汁类", "咖啡类", "冰淇淋类");
		//Style.getSelectionModel().select("奶茶类");
		//sx(ProductionDao.findAll());
		//refresh(goodsDao.findAll());
		b();
		Mealtable.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
			if (Mealtable.getSelectionModel().getSelectedItem()!= null) {
				goods good = (goods)Mealtable.getSelectionModel().getSelectedItem();
				
				//goods_id.setText(good.getId()+"");
			goods_name.setText(good.getName());
				//goods_kind.setText(good.getKind()+"");
				//goods_inventory.setText(good.getInventory()+"");
			goods_price.setText(good.getSellingPrice()+"");
			//	//goods_costPrice.setText(good.getCostPrice()+"");
				//Style.getSelectionModel().select(production.getCategory());
			
			}
		});

	}
	
	public void search(int kind) {
		try {
			
			List<goods> good=new ArrayList<>();
			good=goodsDao.SearchByKind(kind);
			refresh(good);
		}catch(Exception e) {
			//JOptionPane.showMessageDialog(null, "查找数据错误，请重试"); 
			e.printStackTrace();
		}
		
	}
	
	public void refresh(List<goods> Mealtable1) {
		ObservableList<goods> data = FXCollections.observableArrayList(Mealtable1);
		//Goods_id.setCellValueFactory(new PropertyValueFactory<>("id"));
		name.setCellValueFactory(new PropertyValueFactory<>("name"));
		//Goods_kind.setCellValueFactory(new PropertyValueFactory<>("kind"));
		//Goods_inventory.setCellValueFactory(new PropertyValueFactory<>("inventory"));
		price.setCellValueFactory(new PropertyValueFactory<>("sellingPrice"));
		//Goods_costPrice.setCellValueFactory(new PropertyValueFactory<>("costPrice"));
		//goods_id.setText("2");
		//goods_kind.setText("");
		//goods_inventory.setText("");
		goods_price.setText("");
		goods_name.setText("");
		//goods_costPrice.setText("");
	
		Mealtable.setItems(data);
		Mealtable.getSelectionModel().clearSelection();
		
	}
	
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/MainMenu.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }
	
	public void b() {
		refresh(goodsDao.findHave());
	}
	
	public void beef() {
		search(1);
	
	}
	
	public void noodle() {
		search(2);
	}
	
	public void juice() {
		search(3);
	}
	
	public void rice() {
		search(4);
	}
	
	public void snack() {
		search(5);
	}
	
	public void order(){
		String name=goods_name.getText().trim();
		int id=goodsDao.SearchByName(name).getId();
		float i=Float.parseFloat(sales.getText().trim());
		int sale=Integer.parseInt(sales.getText().trim());
		float price= goodsDao.SearchByName(name).getSellingPrice()*sale;
		if(i<=0||i!=sale||i>10) {
			JOptionPane.showMessageDialog(null, "数据错误，请重试"); 
		}
		else {
			orderDetail od=new orderDetail(id, "", sale, price,name);
			goodsDao.UpdateGoods(sale,id);
			checkgood.add(od);
			check1(checkgood);
		//	int orderid=Integer.parseInt(sellorderDao.SearchMaxId().trim())+1;
		//	try {
		//		sellorder so=new sellorder(orderid+"");
		//		Boolean a=sellorderDao.Insert(so);
		//		orderDetail od=new orderDetail(id, name, sale,price);
		//		Boolean b=orderDetailDao.Insert(od);
		//	}catch(Exception e) {
		//		//JOptionPane.showMessageDialog(null, "查找数据错误，请重试"); 
		//		e.printStackTrace();
		}
	}
	public void check1(List<orderDetail> Mealtable1) {
		ObservableList<orderDetail> data = FXCollections.observableArrayList(Mealtable1);
		orderDetail od1=Mealtable1.get(0);
		int id=od1.getId();
		String name=goodsDao.SearchById(id).getName().trim();
		//Goods_id.setCellValueFactory(new PropertyValueFactory<>("id"));
		checkedMeal.setCellValueFactory(new PropertyValueFactory<>("name"));
		//Goods_kind.setCellValueFactory(new PropertyValueFactory<>("kind"));
		//Goods_inventory.setCellValueFactory(new PropertyValueFactory<>("inventory"));
		amount.setCellValueFactory(new PropertyValueFactory<>("sales"));
		//Goods_costPrice.setCellValueFactory(new PropertyValueFactory<>("costPrice"));
		//goods_id.setText("2");
		//goods_kind.setText("");
		//goods_inventory.setText("");
		goods_price.setText("");
		goods_name.setText("");
		sales.setText("");
		//goods_costPrice.setText("");
	
		orderTable.setItems(data);
		orderTable.getSelectionModel().clearSelection();
		
	}	
	
	public void Update() {
		int i=checkgood.size();	
		int now;
		String orderId=sellorderDao.SearchMaxId();
		if(orderId!=null)
			now=Integer.parseInt(orderId.trim())+1;
		else {
			now=1;
			
		}
		orderId=now+"";
		
		sellorder so=new sellorder(orderId);
		sellorderDao.Insert(so);
		System.out.print("第"+now+"号订单已经下单");
		float price=0;
		for(int j=0;j<i;j++) {
			orderDetail od=checkgood.get(j);
			od.setOrderId(orderId);
			price+=od.getPrice();
			String name=goodsDao.SearchById(od.getId()).getName();
			od.setName(name);
			orderDetailDao.Insert(od);
			System.out.print("第"+od.getOrderId()+"号详细订单，商品id为"+od.getId()+"");
		}
		sellorderDao.alterPrice(price,orderId);
		System.out.print("第"+now+"号订单价格为"+price);
		so.setTotal(price);
		Boolean b=sellorderDao.Update(so);
	
		checkgood.clear();
		JOptionPane.showMessageDialog(null, "下单成功！现在进入支付界面。"); 
		
		 FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("View1/Payment.fxml"));
		 PaymentController payController = fxmlLoader.getController();
		
       payController.model.setText(orderId);
		
		try {
			 Stage payStage =(Stage)Update.getScene().getWindow();//传递到主界面
	            //读入布局
	       
	            Parent root = fxmlLoader.load();
	            Scene scene = new Scene(root);
	           
	    
	            payStage.setScene(scene);
	            payStage.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
}
	
	
	
	

